package com.acme.bankapp.service.bank;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.Gender;
import com.acme.bankapp.domain.bank.SavingAccount;
import com.acme.bankapp.domain.bank.exceptions.BankException;

public class ClientBank {
		public static void main(String[] args) {
			try (
					Socket s1 = new Socket("127.0.0.1", 1234);
					PrintWriter writer = new PrintWriter(s1.getOutputStream(), true);
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(s1.getInputStream()));
					){
				Bank bank = new Bank();
				bank.addClient(new Client(Gender.MALE, "Ivanov",
						new SavingAccount(35)));
				bank.addClient(new Client(Gender.FEMALE, "Ivanov",
						new SavingAccount(35)));
				
				for (Client cli: bank.getClients()) {
					for (String str: cli.unload()) {
						writer.println(str);
					}
				}
				writer.println("End communication");
				String responce;
				while ((responce = reader.readLine()) != null) {
					System.out.println(responce);
				}
			}  catch (IOException e) {
				System.out.println("Connection problem");
				e.printStackTrace();
			} catch (BankException e) {
				System.out.println("Problem with bank");
				e.printStackTrace();
			} 

		}

	}
